package com.bamboo.tool.configurable;

import com.bamboo.tool.config.BambooToolComponent;
import com.bamboo.tool.config.RestfulCloudApplicationCache;
import com.bamboo.tool.config.model.ApplicationConfig;
import com.bamboo.tool.constant.StringConstant;
import com.bamboo.tool.entity.BambooDict;
import com.bamboo.tool.store.StoreService;
import com.bamboo.tool.util.I18nUtil;
import com.bamboo.tool.view.component.table.DescFrameWorkTableModel;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.table.JBTable;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class RestfulCloudConfigurable implements Configurable {
    private JPanel mainPanel;

    private JPanel incPanel;
    private JPanel descriptionPanel;
    private JCheckBox descriptionBox;
    private JPanel descFrameWorkPanel;
    private JLabel descFrameWorkLable;
    private DescFrameWorkTableModel defaultTableModel;
    private @NotNull JPanel descFrameWorkPane;
    private TextFieldWithBrowseButton dataStorageAddress;
    private TextFieldWithBrowseButton exportDefaultPath;

    List<BambooDict> descFrameworks;

    @Override
    public String getDisplayName() {
        return "RestfulCloud";
    }

    @Override
    public @Nullable JComponent createComponent() {
        boolean isShowDesc = BambooToolComponent.getStoreService().selectIsShowDesc();
        ApplicationConfig state = RestfulCloudApplicationCache.getInstance().getState();

        String exportDefaultPathStr = state.getExportDefaultPath();
        incPanel.setBorder(IdeBorderFactory.createTitledBorder(I18nUtil.getString("setting.interfaceDescConfig"), true));
        descriptionBox.setText(I18nUtil.getString("setting.isOpenInterfaceDesc"));
        descriptionBox.setSelected(isShowDesc);
        descFrameWorkPanel.setBorder(IdeBorderFactory.createTitledBorder(I18nUtil.getString("setting.interfaceDescFrameworkSettings"), true));
        descFrameWorkLable.setText(I18nUtil.getString("setting.descFrameWorkLable"));
        JBTable descFrameWorkTable = new JBTable();
        List<BambooDict> descSort = BambooToolComponent.getStoreService().getAllDictByCode(Arrays.asList(StringConstant.DESC_SORT));
        descFrameworks = descSort.stream().sorted(Comparator.comparing(BambooDict::getDescription)).collect(Collectors.toList());

        defaultTableModel = new DescFrameWorkTableModel(descFrameworks);
        defaultTableModel.addColumn(I18nUtil.getString("setting.defaultTableModel.framework"));

        for (BambooDict descFramework : descFrameworks) {
            defaultTableModel.addRow(descFramework.getStrings());
        }
        descFrameWorkTable.setModel(defaultTableModel);
        final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(descFrameWorkTable);
        decorator.disableAddAction();
        decorator.disableRemoveAction();
        descFrameWorkPane.add(decorator.createPanel(), BorderLayout.CENTER);
        descFrameWorkTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        descFrameWorkTable.setBorder(null);

        // 导出默认路径
        exportDefaultPath.addBrowseFolderListener(new TextBrowseFolderListener(FileChooserDescriptorFactory.createSingleFolderDescriptor(), null));
        exportDefaultPath.setText(exportDefaultPathStr);

        return mainPanel;
    }

    @Override
    public boolean isModified() {
        if (defaultTableModel.isModified()) {
            return true;
        }
        boolean isShowDesc = BambooToolComponent.getStoreService().selectIsShowDesc();
        boolean selected = descriptionBox.isSelected();
        if (isShowDesc != selected) {
            return true;
        }
        ApplicationConfig state = RestfulCloudApplicationCache.getInstance().getState();

        String exportDefaultPathStr = state.getExportDefaultPath();
//        if (dataStorageAddress.getText() != null && !dataStorageAddress.getText().equals(dataPath)) {
//            return true;
//        }
        if (exportDefaultPath.getText() != null && !exportDefaultPath.getText().equals(exportDefaultPathStr)) {
            return true;
        }

        return false;
    }

    @SneakyThrows
    @Override
    public void apply() {
        StoreService storeService = BambooToolComponent.getStoreService();
        storeService.updateDescFrameworkSequence(descFrameworks);
        storeService.updateIsShowDesc(descriptionBox.isSelected());
        List<BambooDict> descSort = BambooToolComponent.getStoreService().getAllDictByCode(Arrays.asList(StringConstant.DESC_SORT));
        List<BambooDict> dicts = descSort.stream().sorted(Comparator.comparing(BambooDict::getDescription)).collect(Collectors.toList());
        defaultTableModel.setDataList(dicts);
        ApplicationConfig state = RestfulCloudApplicationCache.getInstance().getState();

        if (!state.getExportDefaultPath().equals(exportDefaultPath.getText())) {
            state.setExportDefaultPath(exportDefaultPath.getText());
        }
    }
}
